
/* GENERATE RESULTS TO REPORT IN SUMMARY TABLES */

set trace on

foreach f in "tax" "subsidy" "subsidy_e_p90" "subsidy_h_p85" {
forvalues num = 25(1)45  {

	if "`f'"=="tax" {
		local suffix = "_t"
	}
	else if "`f'"=="subsidy" {
		local suffix = "_s"
	}
	else if "`f'"=="subsidy_h" {
		local suffix = "_sh"
	}
	else if "`f'"=="subsidy_p75" {
		local suffix = "_p75"
	}
	else if "`f'"=="subsidy_p90" {
		local suffix = "_p90"
	}
	else if "`f'"=="subsidy_h_p75" {
		local suffix = "_p75h"
	}
	else if "`f'"=="subsidy_h_p85" {
		local suffix = "_p85h"
	}
	else if "`f'"=="subsidy_h_p90" {
		local suffix = "_p90h"
	}
	else if "`f'"=="subsidy_i_p75" {
		local suffix = "_p75i"
	}
	else if "`f'"=="subsidy_i_p90" {
		local suffix = "_p90i"
	}
	else if "`f'"=="subsidy_ih_p75" {
		local suffix = "_p75ih"
	}
	else if "`f'"=="subsidy_ih_p90" {
		local suffix = "_p90ih"
	}
	else if "`f'"=="subsidy_e_p90" {
		local suffix = "_p90e"
	}
	if `num' != 25 {
		local suffix = "_`num'`suffix'"
	}
	
	use "$result_simul/simulation_allspec_`f'_`num'.dta", clear

	*unique naics if subsidy_rate > 0 & eivar=="ei"
	
	
	encode eivar, gen(eivar_num)
	unique naics if subsidy_rate > 0 & eivar_num==1

	gen num_industries = r(unique) if eivar=="ei"
	
	
	*unique naics if subsidy_rate > 0 & eivar=="ei_tot"
	
	unique naics if subsidy_rate > 0 & eivar_num==2
	
	replace num_industries = r(unique) if eivar=="ei_tot"
	
	
	keep  spec carbonTax eivar model *_mn leakage_agg* prod_agg*  netabatetrate_agg* exp_agg* imp_agg* tax_rev_agg* subsidy_agg* tr_agg* netabate_agg* change_*_agg* abatement_agg* pc_change_*_agg* abatementrate_agg* leakagerate_agg* abatement_agg* num_industries*
	
	duplicates drop
	

	rename pc_change_prod_agg pc_prod`suffix'

	rename pc_change_imp_agg pc_imp`suffix'

	rename pc_change_exp_agg pc_exp`suffix'

	rename leakagerate_agg  leakagerate`suffix'
	
	rename leakage_agg leakage_agg`suffix'

	rename abatementrate_agg abaterate`suffix'

	rename abatement_agg abatement`suffix'

	rename netabate_agg netabate`suffix'

	rename netabate_agg_check netabatechk`suffix'

	rename netabatetrate_agg netabrate`suffix'

	rename tax_rev_agg tax_rev_agg`suffix'

	rename tr_agg tr_agg`suffix'
	
	rename subsidy_agg subsidy_agg`suffix'
	
	rename num_industries num_industries`suffix' 
	* rescale
	foreach var in "pc_prod" "pc_imp" "pc_exp" "abaterate" "leakage_agg" "netabrate"{
		replace `var'`suffix' = `var'`suffix' * 100

	}
	* define abatement as positive
	foreach var in "abaterate" "netabrate" {
		replace `var'`suffix' = - `var'`suffix'
	}
	* convert revenue to net
	replace tax_rev_agg`suffix' = tax_rev_agg`suffix' - subsidy_agg`suffix'
	foreach var in "tax_rev_agg" {
		replace `var'`suffix' = `var'`suffix'/ 1000000000
	}


	keep carbonTax spec eivar model *`suffix' 
	
	if "`f'_`num'"!="tax_25" {
		sort spec
		merge 1:1 spec using temp
		drop _merge
	}
	sort spec
	save temp, replace
}
}
